package com.leap.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leap.entity.Admin;
import com.leap.mapper.AdminMapper;
import com.leap.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Admin admin = this.findByUsername(username);

        if (admin == null) {
            throw new UsernameNotFoundException("未找到账号！");
        }else
            return User.builder()
                .username(username)
                .password(admin.getPassword())
                .authorities(admin.getRole())
                .build();

    }

    @Override
    public Admin findByUsername(String username) {

        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername, username);

        Admin admin = this.getOne(queryWrapper);

        return admin;
    }
}
